﻿using System;
using System.Collections.Generic;
using EnvDTE;


namespace CiStatus.CiStatusAddIn.Logic
{
    public class SettingsResolver
    {
        private readonly ISettingsFormatter _formatter;
        private readonly Properties _properties;
        private IDictionary<KeyValuePair<string, string>, IDictionary<string, string>> _settings;

        public SettingsResolver(Properties properties, ISettingsFormatter formatter)
        {
            _formatter = formatter;
            _properties = properties;
            Resolve();
        }

        private void Resolve()
        {
            foreach (var property in _properties)
            {
                string name = ((dynamic) property).Name;

                if (name.Equals("Settings"))
                {
                    var value = ((dynamic) property).Value;
                    _settings = _formatter.Deserialize(value);
                }

                if (name.Equals("PluginPath"))
                {
                    //Ugly as hell, but I dunno wath to do with this shit...
                    try
                    {
                        string s = ((dynamic)property).Value;
                        if (!string.IsNullOrEmpty(s))
                            PluginPath = s;
                    }
                    catch (Exception){}
                }

                if (name.Equals("PollingFrequence"))
                {
                    int freq = ((dynamic) property).Value;
                    PollingFrequency = freq < 1 ? 60 : freq;
                }
            }
        }

        public int PollingFrequency { get; set; }

        public IDictionary<KeyValuePair<string, string>, IDictionary<string, string>> Settings
        {
            get { return _settings; }
        }

        public string PluginPath { get; set; }
    }
}
